
import { crtChannel } from "./crtChannel";
import { crtChannelHw } from "./crtChannelHw";


/**
 * Created by 席庆功
 * Time: 2024/01/15.
 */
const { ccclass, property } = cc._decorator;

@ccclass
export class nativeInterAd extends cc.Component {

    @property(cc.Label)
    title: cc.Label = null;
    @property(cc.Label)
    source: cc.Label = null;
    @property(cc.Label)
    clickBtnTxt: cc.Label = null;
    @property(cc.Sprite)
    picUri: cc.Sprite = null;
    @property(cc.Node)
    bg: cc.Node = null;
    @property(cc.Node)
    unqiuebg: cc.Node = null;
    isBanner = false
    public adid: string = null;
    public init(adid: string, picUri: string, title: string, source: string, clickBtnTxt: string, isbanner = false) {
        // console.log('init',adid,picUri,title,source,clickBtnTxt)
        // if(pzaChannel.instance().channel == 'hw'){

        this.node.y = 100;
        // }
        this.title.string = title;
        this.source.string = source;
        this.clickBtnTxt.string = clickBtnTxt;
        this.adid = adid;
        // this.clickUrl=clickUrl;

        this.bg.on(cc.Node.EventType.TOUCH_END, this.clickAd, this)
        //pzaWorldEventManager.addListener(pzaWorldEventType.REPORT_AD_SHOW, this.reportAdShow, this);
        if (isbanner) {
            this.isBanner = true
        } else {
            crtChannel.getChannel().doAction('插屏一定会被点击', ['hw', (flag) => {
                console.log("bg__________________", flag)
                this.unqiuebg.active = flag
            }]);
            crtChannel.getChannel().doAction('有插屏', ['hw'])
        }

        this.loadPics(picUri)
        this.reportAdShow();

    }

    protected onDisable(): void {
        this.bg.off(cc.Node.EventType.TOUCH_END, this.clickAd, this)
        //pzaWorldEventManager.removeListener(pzaWorldEventType.REPORT_AD_SHOW, this.reportAdShow, this);
    }

    reportAdShow() {
        // console.log('reportAdShow adid:',this.adid)
        // console.log('reportAdShow clickUrl:',this.clickUrl)
        if (this.adid) {

            (crtChannel.getChannel() as crtChannelHw).reportAdShow(this.adid, this.isBanner);
        }
    }

    clickAd() {
        console.log("点击广告")
        if (this.adid) {
            (crtChannel.getChannel() as crtChannelHw).reportAdClick(this.adid, this.isBanner);

        }

        this.close();
    }

    loadPics(picUri) {
        if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
            return
        }
        // console.log('loadPics')
        //remoteUrl 获取到的图片地址
        let remoteUrl = picUri
        let type = '.jpg';
        if (remoteUrl.indexOf('.png') != -1) {
            type = '.png';
        } else if (remoteUrl.indexOf('.jpg') != -1) {
            type = '.jpg';
        } else if (remoteUrl.indexOf('.jpeg') != -1) {
            type = '.jpeg';
        }
        // type = '.image'
        var self = this;
        // console.log("remoteUrl "+remoteUrl);
        // console.log("type "+type);
        // console.log('cc',cc)
        // console.log('cc.asset',cc.assetManager)
        // console.log('cc.assetManager.loadRemote',cc.assetManager.loadRemote)
        // cc.assetManager.loadRemote(remoteUrl,{ext:type},(err, res: any)=>{
        //     if (err) {
        //         console.log('err :',err);
        //         // pzakc.err('原生图片加载失败', type, err);
        //         self.close();
        //         return;
        //     }
        //     // console.log("err"+err);
        //     console.log('setSP')
        //     var spriteFrame=new cc.SpriteFrame(res);
        //     self.picUri.spriteFrame=spriteFrame;
        // });

        const img = new Image();
        img.src = remoteUrl;
        img.crossOrigin = 'anonymous';
        img.onload = (e) => {
            // console.log('load',remoteUrl, img, e);
            const texture = new cc.Texture2D();
            texture.initWithElement(img);
            self.picUri.spriteFrame = new cc.SpriteFrame(texture);
        }
    }

    public close(): void {
        console.log("关闭原生")
        this.scheduleOnce(() => {
            this.node.destroy()
        }, 0.1)



    }
    protected onDestroy(): void {
        if (!this.isBanner) {
            crtChannel.getChannel().doAction('插屏间隔', ['hw'])
            crtChannel.getChannel().doAction('无插屏', ['hw'])
        }

        if (this.isBanner) {
            setTimeout(() => {

                (crtChannel.getChannel() as crtChannelHw).showBanner()
            }, 30000);
        }
    }
}
